<?php
/**
 * Created by PhpStorm.
 * User: Jessewang
 * Date: 2017/3/29
 * Time: 上午11:34
 */


namespace App\Libs;
use Swoole;
use Swoole\Client\CURL;
use Swoole\Client\Http;
class WeiBoApi
{
    const OAUTH_PREFIX = 'https://api.weibo.com/2/users/show.json';

    public  $appid=null;
    public  $appkey = null;

    function  __construct()
    {
        $config = \Swoole::$php->config["sns"];

        $this->appid = $config['qq']['appid'];
        $this->appkey = $config['qq']['appkey'];
    }

    function  getUserInfo($accesstoken,$openid)
    {
        $httpClient = new CURL();
        $data = array(
            "access_token"=>$accesstoken,
            'uid'=>$openid,
        );
        $ret = $httpClient->get("https://api.weibo.com/2/users/show.json?".$this->buildQueryString($data));
       // var_dump($ret);
        return json_decode($ret);
    }

    function buildQueryString($data)
    {
        $querystring = '';
        if (is_array($data)) {
            // Change data in to postable data
            foreach ($data as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $val2) {
                        $querystring .= urlencode($key) . '=' . urlencode($val2) . '&';
                    }
                } else {
                    $querystring .= urlencode($key) . '=' . urlencode($val) . '&';
                }
            }
            $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
        } else {
            $querystring = $data;
        }
        return $querystring;
    }
}
